package com.liser.southernstar.ui.adapter;


import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.kunminx.linkage.adapter.viewholder.LinkagePrimaryViewHolder;
import com.kunminx.linkage.contract.ILinkagePrimaryAdapterConfig;
import com.liser.southernstar.R;

/**
 * 商品主菜单适配器
 *
 * @author LISER
 * @date 2020-01-11 16:05
 */
public class GoodsPrimaryAdapter implements ILinkagePrimaryAdapterConfig {

    private static final int MARQUEE_REPEAT_LOOP_MODE = -1;
    private static final int MARQUEE_REPEAT_NONE_MODE = 0;
    private Context mContext;
    private OnPrimaryItemClickListener mItemClickListener;

    private int position;

    public GoodsPrimaryAdapter() {

    }

    public GoodsPrimaryAdapter setOnItemClickListener(OnPrimaryItemClickListener itemClickListener) {
        mItemClickListener = itemClickListener;
        return this;
    }

    @Override
    public void setContext(Context context) {
        mContext = context;
    }

    @Override
    public int getLayoutId() {
        return R.layout.adapter_goods_primary;
    }

    @Override
    public int getGroupTitleViewId() {
        return R.id.tv_group;
    }

    @Override
    public int getRootViewId() {
        return R.id.layout_group;
    }

    @Override
    public void onBindViewHolder(LinkagePrimaryViewHolder holder, boolean selected, String title) {
        TextView tvTitle = ((TextView) holder.mGroupTitle);
        tvTitle.setText(title);

        if (selected) {
            holder.mLayout.setBackgroundColor(Color.WHITE);
            tvTitle.setTextColor(Color.BLACK);
            tvTitle.setTypeface(Typeface.DEFAULT_BOLD);
        } else {
            holder.mLayout.setBackgroundColor(ContextCompat.getColor(mContext, R.color.type_gray));
            tvTitle.setTextColor(ContextCompat.getColor(mContext, R.color.type_normal));
            tvTitle.setTypeface(Typeface.DEFAULT);
        }

        tvTitle.setBackgroundColor(ContextCompat.getColor(mContext, selected ? R.color.color_goods_primary : R.color.colorWhite));
        tvTitle.setTextColor(ContextCompat.getColor(mContext, selected ? R.color.colorWhite : R.color.colorGray));
        tvTitle.setEllipsize(selected ? TextUtils.TruncateAt.MARQUEE : TextUtils.TruncateAt.END);
        tvTitle.setFocusable(selected);
        tvTitle.setFocusableInTouchMode(selected);
        tvTitle.setMarqueeRepeatLimit(selected ? MARQUEE_REPEAT_LOOP_MODE : MARQUEE_REPEAT_NONE_MODE);
        tvTitle.setMarqueeRepeatLimit(MARQUEE_REPEAT_LOOP_MODE);
    }

    @Override
    public void onItemClick(LinkagePrimaryViewHolder holder, View view, String title) {
        if (holder.getAdapterPosition() != position) {
            holder.mLayout.setSelected(true);
        }

        if (mItemClickListener != null) {
            mItemClickListener.onPrimaryItemClick(holder, view, title);
        }
        position = holder.getAdapterPosition();
    }

    public interface OnPrimaryItemClickListener {
        /**
         * we suggest you get position by holder.getAdapterPosition
         *
         * @param holder primaryHolder
         * @param view   view
         * @param title  groupTitle
         */
        void onPrimaryItemClick(LinkagePrimaryViewHolder holder, View view, String title);
    }
}
